const Actions = require('./actions');
const DB = require('app/db.js');
import pinyin from 'simple-pinyin';
import { Storage } from 'clientConfig/util/StoreData'
const getPhonetic = (value) => {
    return pinyin(value.name, { pinyinOnly: false });
}
import { Toast } from 'saltui';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        isBzr: null,//是不是该班班主任
        listdata: null,//班级列表
        classId: null,//当前选中的班级id
        select_str: '',//当前选中字母表
        data_studentList: {},//学生列表
        select_str_obj: {},//字面列表
        isBzr_obj: {},//班主任列表

    },
    onGetClassList: function (params, cb) {
        let t = this;
        // if (t.data.listdata && t.data.listdata.length > 0 && t.data.classId) {
        //     t.data.studentList = t.data.data_studentList[t.data.classId];
        //     t.updateComponent();
        //     console.log(0)
        //     let list = [t.data.studentList];
        //     const hasNoMore = list.length < t.data.pageSize;
        //     let success = t.data.success;
        //     success(hasNoMore, list);
        //     // t.updateComponent();
        //     return;
        // }
        DB.Teacher.get_class_lists({
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId')
        })
            .then((content) => {
                if (content.code == 1) {
                    let schoolId = content.check.schoolId;
                    let teacherId = content.check.teacherId;
                    t.data.isBzr = content.data.isBzr;
                    content = content.data.list;
                    var list = [];
                    var obj = {};
                    if (content && content.length > 0) {
                        for (let item of content) {
                            obj.imgUrl = '';
                            obj.classId = item.id;
                            obj.title = item.name;
                            list.push(obj);
                            obj = {};
                        }
                        t.data.classList = list;
                        //存储数据
                        t.data.listdata = list;
                        if (!t.data.classId) {
                            t.data.classId = list[0].classId;
                        }
                        t.updateComponent();
                    }

                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onGetMyclassLists: function (pageNum, props, success, error) {
        let t = this;
        if (t.data.data_studentList && t.data.data_studentList.hasOwnProperty(props.classId) && t.data.select_str && t.data.isBzr) {
            t.data.studentList = t.data.data_studentList[props.classId];
            t.data.classId = props.classId;
            t.data.isBzr = t.data.isBzr_obj[props.classId];
            let list = [t.data.studentList];
            const hasNoMore = list.length < this.data.pageSize;
            let success = this.data.success;
            success(hasNoMore, list);
            t.data.select_str = t.data.select_str_obj[props.classId];
            t.updateComponent();
            return;
        }
        DB.Teacher.get_myclass_lists({
            schoolId: Storage.get('user.schoolId'),
            classId: props.classId,
            teacherId: Storage.get('user.teacherId')
        })
            .then((content) => {
                let schoolId = Storage.get('user.schoolId');
                if (content.code == 1) {
                    let teacherId = content.check.teacherId;
                    let classId = content.check.classId;
                    t.data.classId = classId;
                    t.data.isBzr = content.data.isBzr;
                    t.data.isBzr_obj[classId] = content.data.isBzr;
                    content = content.data.list;
                    if (content && content.length > 0) {
                        var obj = {};
                        var select_str = ''
                        for (let item of content) {
                            item.phonetic = getPhonetic(item);
                            item.schoolId = schoolId;
                            if (!obj.hasOwnProperty(item.phonetic[0][0])) {
                                let data_arr = [];
                                data_arr.push(item);
                                obj[item.phonetic[0][0]] = data_arr;
                                select_str += item.phonetic[0][0];
                            } else {
                                let data_arr = obj[item.phonetic[0][0]];
                                data_arr.push(item);
                                obj[item.phonetic[0][0]] = data_arr;
                            }
                        }
                        //存储数据
                        t.data.data_studentList[classId] = obj;
                        t.data.select_str = select_str;
                        t.data.select_str_obj[classId] = select_str;
                        let list = [obj];
                        const hasNoMore = list.length < props.pageSize;
                        if (success) {
                            this.data.pageSize = props.pageSize;
                            this.data.success = success;
                            success(hasNoMore, list);
                        } else if (!content || !success) {
                            error();
                        }
                        t.updateComponent();
                    }                  
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
